﻿#ifndef nalu_h
#define nalu_h
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include "bs.h"

#define MAX_NALU_SIZE 8000000         // 最大nalu


struct signal_nalu {
	signal_nalu() {
		m_num = -999;
		m_len = -999;
		m_nalu_ref_idc = -999;
		m_nal_type = -999;
		m_first_mb_in_slice = -999;
		m_slice_type = -999;
		m_slice_qp_delta = -999;
		m_pic_init_qp_minus26 = -999;
	}
	int m_num;
	int m_len;
	int m_nalu_ref_idc;
	int m_nal_type;
	int m_first_mb_in_slice;
	int m_slice_type;
	float m_slice_qp_delta;
	int m_pic_init_qp_minus26;
};


typedef struct
{
	// nal header
	int forbidden_zero_bit;                     // f(1)
	int nal_ref_idc;                            // u(2)
	int nal_unit_type;                          // u(5)
	int len;                // 最开始保存nalu_size, 然后保存rbsp_size,最终保存SODB的长度
	uint8_t *buf;
} nalu_t;

/* 7.4.1 Table 7-1 NAL unit types */
enum nal_unit_type {
	H264_NAL_UNKNOWN = 0,
	H264_NAL_SLICE = 1,
	H264_NAL_DPA = 2,
	H264_NAL_DPB = 3,
	H264_NAL_DPC = 4,
	H264_NAL_IDR_SLICE = 5,
	H264_NAL_SEI = 6,
	H264_NAL_SPS = 7,
	H264_NAL_PPS = 8,
	H264_NAL_AUD = 9,
	H264_NAL_END_SEQUENCE = 10,
	H264_NAL_END_STREAM = 11,
	H264_NAL_FILLER_DATA = 12,
	H264_NAL_SPS_EXT = 13,
	H264_NAL_AUXILIARY_SLICE = 19,
};

int find_nal_unit(nalu_t *nalu, int buff_size, int *curr_nal_start, int *curr_find_index);
void read_nal_unit(nalu_t *nalu, signal_nalu &sig_nalu);

int nal_to_rbsp(nalu_t *nalu);
int rbsp_to_sodb(nalu_t *nalu);

// 在rbsp_trailing_bits()之前是否有更多数据
int more_rbsp_data(bs_t *b);

nalu_t *allocNalu(int buff_size);
void freeNalu(nalu_t *nalu);

#endif /* nalu_h */
